package com.bridgeintelligent.tag.customer.controller;

import com.bridgeintelligent.tag.customer.entity.CustomersPage;
import com.bridgeintelligent.tag.customer.entity.Result;
import com.bridgeintelligent.tag.customer.exception.CustomersException;
import com.bridgeintelligent.tag.customer.model.IssueModel;
import com.bridgeintelligent.tag.customer.service.IIssueApproveService;
import com.bridgeintelligent.tag.customer.vo.IssueVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description
 * @author: grp 2021-02-23 23:21
 **/
@Api(tags = "下发审核")
@RestController
@AllArgsConstructor
@Validated
@Slf4j
@RequestMapping("api/issueApprove")
public class IssueApproveController {

    IIssueApproveService issueApproveService;


    @ApiOperation(value = "下发提交审批", notes = "下发客群提交审批")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "issue", value = "SET设置")
    })
    @PostMapping("/addCommit")
    public Result addCommit(@RequestBody IssueModel issue) {
        log.info("issue="+issue.toString());
        try{
            issueApproveService.addCommit(issue);
        } catch (CustomersException e) {
            return new Result(e.getCode(), e.getMessage());
        } catch (Exception e){
            log.error("客群下发 提交审核 出错", e);

            return new Result(999,"客群下发提交审核出错");
        }
        return new Result(0);
    }

    @ApiOperation(value = "下发客群审批通过", notes = "下发客群审批通过")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "customerId", value = "客群id"),
            @ApiImplicitParam(paramType = "query", name = "instanceId", value = "流程实例ID"),
            @ApiImplicitParam(paramType = "query", name = "applicant", value = "申请人ID")
    })
    @PostMapping("addPass")
    public Result addPass(String customerId, String instanceId, String applicant) {
        issueApproveService.addPass(customerId, instanceId, applicant);
        return new Result(0);
    }

    @ApiOperation(value = "审批驳回", notes = "下发客群审批驳回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "customerId", value = "客群id"),
            @ApiImplicitParam(paramType = "query", name = "instanceId", value = "流程实例ID"),
            @ApiImplicitParam(paramType = "query", name = "applicant", value = "申请人ID"),
            @ApiImplicitParam(paramType = "query", name = "desc", value = "驳回原因")
    })
    @PostMapping("reject")
    public Result reject(String customerId, String applicant, String instanceId, String desc) {
        issueApproveService.reject(customerId, applicant,instanceId, desc);
        return new Result(0);
    }

    /**
     * 审批列表
     *
     * @param pageNumber
     * @param pageSize
     * @param customerName
     * @param approveType
     * @param approveState
     * @param uptTimeStart
     * @param uptTimeEnd
     * @return
     */
    @ApiOperation(value = "审批列表", notes = "组合标签审批列表")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "pageNumber", value = "页码"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数"),
            @ApiImplicitParam(paramType = "query", name = "customerName", value = "客群名"),
            @ApiImplicitParam(paramType = "query", name = "approveType", value = "申请类型"),
            @ApiImplicitParam(paramType = "query", name = "approveState", value = "审批状态"),
            @ApiImplicitParam(paramType = "query", name = "uptTimeStart", value = "更新时间筛选（开始）"),
            @ApiImplicitParam(paramType = "query", name = "uptTimeEnd", value = "更新时间筛选（结束）")
    })
    @GetMapping("findList")
    public Result findList(Integer pageNumber, Integer pageSize, String customerName, String approveType, String approveState, String uptTimeStart, String uptTimeEnd) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        pageSize = pageSize == null ? 10 : pageSize;
        CustomersPage<IssueVO> customerPage = issueApproveService.findList(pageNumber, pageSize, customerName, approveType, approveState, uptTimeStart, uptTimeEnd);
        return new Result(0, customerPage);
    }

    @ApiOperation(value = "下发重新提交审批", notes = "下发重新提交审批")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "下发id")
    })
    @PostMapping("/resubmit")
    public Result resubmit(String id) {
        issueApproveService.resubmit(id);
        return new Result(0);
    }
}
